package com.tanhua.admin.listener;

import com.alibaba.fastjson.JSON;
import com.tanhua.admin.mapper.LogMapper;
import com.tanhua.model.domain.Log;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @Description: test
 * @Create by: JJ菜菜
 * @Date: 2021/12/10 10:05
 *
 *  监听器，处理器，消息监听器，监听日志消息
 *
 */

@Component
public class LogListener {


    @Autowired
    private LogMapper logMapper;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "tanhua.log.queue", durable = "true"),
            exchange = @Exchange(value = "tanhau.log.exchange", type = ExchangeTypes.TOPIC),
            key = {"logs.*"}
    ))
    public void listenCreate(String message){

        try {
            // 从rebbit中获取消息
            Map<String, Object> map = JSON.parseObject(message);
            // 1. 获取数据
        /*Long userId = (Long)map.get("userId");
        String type = (String)map.get("type");
        String logTime = (String) map.get("logTime");
        String busId = (String)map.get("busId");
        Log log = new Log(userId, logTime, type);*/

            Long userId = (Long) map.get("userId");
            String date = (String) map.get("date");
            String objId = (String) map.get("objId");
            String type = (String) map.get("type");
            Log log = new Log(userId, date,  type);
            logMapper.insert(log);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
